package control;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.domain.JADEAgentManagement.KillContainer;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.util.ArrayList;

import model.Coordenada;
import model.Rota;
import agent.AreaRotas;

public class ControleAgentes {
	
	
		jade.core.Runtime runtime;
		Profile perfilMain;
		ContainerController mainContainer;
		AgentController agent;
		AgentController agent0;
		AgentController agent1;
		AgentController agent2;
		AgentController agent3;
		AgentController agent4;
		AgentController agent5;
		AgentController agent6;
		AgentController agent7;
		AgentController agent8;
		AgentController agent9;
		
		
		public ControleAgentes(){
			runtime = jade.core.Runtime.instance();
			perfilMain = new ProfileImpl();
			mainContainer = runtime.createMainContainer(perfilMain);
			
			try{
				agent0 = mainContainer.createNewAgent("Buscador0", "agent.AgBuscador", null);
				agent0.start();
				agent1 = mainContainer.createNewAgent("Buscador1", "agent.AgBuscador", null);
				agent1.start();
				agent2 = mainContainer.createNewAgent("Buscador2", "agent.AgBuscador", null);
				agent2.start();
				agent3 = mainContainer.createNewAgent("Buscador3", "agent.AgBuscador", null);
				agent3.start();
				agent4 = mainContainer.createNewAgent("Buscador4", "agent.AgBuscador", null);
				agent4.start();
				agent5 = mainContainer.createNewAgent("Buscador5", "agent.AgBuscador", null);
				agent5.start();
				agent6 = mainContainer.createNewAgent("Buscador6", "agent.AgBuscador", null);
				agent6.start();
				agent7 = mainContainer.createNewAgent("Buscador7", "agent.AgBuscador", null);
				agent7.start();
				agent8 = mainContainer.createNewAgent("Buscador8", "agent.AgBuscador", null);
				agent8.start();
				agent9 = mainContainer.createNewAgent("Buscador9", "agent.AgBuscador", null);
				agent9.start();
				
			
				
		
				//agent1.kill();
				//agent2.kill();
				//main.kill();
				//runtime.shutDown();
				
				
			}catch(StaleProxyException e){
				e.printStackTrace();
			}

		
			
		}
		
		public void iniciaAgentes(Coordenada origem, Coordenada destino){
			ArrayList<Rota> rotas = new ArrayList<Rota>();
			AreaRotas.setRotas(rotas);
			Object[] argumentos = new Object[2];
			argumentos[0] = origem;
			argumentos[1] = destino;
			
			try{
				agent = mainContainer.createNewAgent("Coordenador", "agent.AgCoordenador", argumentos);
				agent.start();
			}catch(StaleProxyException e){
				e.printStackTrace();
			}
			

		}
		
		
		public void paraJade(){
			try{
				
				this.agent.kill();
				this.agent0.kill();
				this.agent1.kill();
				this.agent2.kill();
				this.agent3.kill();
				this.agent4.kill();
				this.agent5.kill();
				this.agent6.kill();
				this.agent7.kill();
				this.agent8.kill();
				this.agent9.kill();
				this.mainContainer.kill();
				this.runtime.shutDown();
				
			}catch(StaleProxyException e){
				e.printStackTrace();
			}
		}

}
